package com.pig4cloud.pig.module.controller.titleMenu;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.location.City;
import com.pig4cloud.pig.module.api.entity.location.Province;
import com.pig4cloud.pig.module.api.entity.titleMenu.TitleMenuSectionCity;
import com.pig4cloud.pig.module.service.location.CityService;
import com.pig4cloud.pig.module.service.location.ProvinceService;
import com.pig4cloud.pig.module.service.titleMenu.ITitleMenuSectionCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 行所显示的城市Controller
 *
 * @author ruoyi
 * @date 2022-12-10
 */
@Validated
@Api(value = "行所显示的城市控制器", tags = {"行所显示的城市管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/titleMenu/section/city")
public class TitleMenuSectionCityController {

    private final ITitleMenuSectionCityService titleMenuSectionCityService;
	private final ProvinceService provinceService;
	private final CityService cityService;

    /**
     * 查询行所显示的城市列表
     */
    @ApiOperation("查询行所显示的城市列表")
    @PreAuthorize("@pms.hasPermission('module:sectionsCity:list')")
    @GetMapping("/list")
    public TableDataInfo<TitleMenuSectionCity> list(@Validated TitleMenuSectionCity titleMenuSectionCity) {
        QueryWrapper<TitleMenuSectionCity> qw = new QueryWrapper<>();
        if(titleMenuSectionCity.getMenuSectionId()!=null){
        	qw.eq("menu_section_id",titleMenuSectionCity.getMenuSectionId());
		}

        TableDataInfo<TitleMenuSectionCity> td = PageUtils.buildDataInfo(titleMenuSectionCityService.page(PageUtils.buildPage(),qw));
        List<TitleMenuSectionCity> list = td.getData();
        return td;
    }

    /**
     * 获取行所显示的城市详细信息
     */
    @ApiOperation("获取行所显示的城市详细信息")
    @PreAuthorize("@pms.hasPermission('module:sectionsCity:query')")
    @GetMapping("/{id}")
    public R<TitleMenuSectionCity> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        TitleMenuSectionCity titleMenuSectionCity = titleMenuSectionCityService.getById(id);
        if(titleMenuSectionCity!=null){
			String[] areas = { titleMenuSectionCity.getProvId()+"",titleMenuSectionCity.getCityId()+""};
			titleMenuSectionCity.setProvinceArray(areas);
		}
        return R.ok(titleMenuSectionCity);
    }

    /**
     * 新增行所显示的城市
     */
    @ApiOperation("新增行所显示的城市")
    @PreAuthorize("@pms.hasPermission('module:sectionsCity:add')")
    @SysLog("行所显示的城市")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody TitleMenuSectionCity titleMenuSectionCity) {

		if(titleMenuSectionCity.getProvinceArray()!=null&&titleMenuSectionCity.getProvinceArray().length==2){

			Province province = provinceService.findById(titleMenuSectionCity.getProvinceArray()[0]);
			if(province!=null){
				titleMenuSectionCity.setProvName(province.getProvinceName());
				titleMenuSectionCity.setProvId(province.getId());
			}

			City city = cityService.findById(titleMenuSectionCity.getProvinceArray()[1]);
			if(city!=null){
				titleMenuSectionCity.setCityName(city.getCityName());
				titleMenuSectionCity.setCityId(city.getId());
			}
		}

		return R.okByRow(titleMenuSectionCityService.save(titleMenuSectionCity) ? 1 : 0);
    }

    /**
     * 修改行所显示的城市
     */
    @ApiOperation("修改行所显示的城市")
    @PreAuthorize("@pms.hasPermission('module:sectionsCity:edit')")
    @SysLog("行所显示的城市")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody TitleMenuSectionCity titleMenuSectionCity) {

		if(titleMenuSectionCity.getProvinceArray()!=null&&titleMenuSectionCity.getProvinceArray().length==2){

			Province province = provinceService.findById(titleMenuSectionCity.getProvinceArray()[0]);
			if(province!=null){
				titleMenuSectionCity.setProvName(province.getProvinceName());
				titleMenuSectionCity.setProvId(province.getId());
			}

			City city = cityService.findById(titleMenuSectionCity.getProvinceArray()[1]);
			if(city!=null){
				titleMenuSectionCity.setCityName(city.getCityName());
				titleMenuSectionCity.setCityId(city.getId());
			}
		}
        return R.okByRow(titleMenuSectionCityService.updateById(titleMenuSectionCity) ? 1 : 0);
    }

    /**
     * 删除行所显示的城市
     */
    @ApiOperation("删除行所显示的城市")
    @PreAuthorize("@pms.hasPermission('module:sectionsCity:remove')")
    @SysLog("行所显示的城市")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(titleMenuSectionCityService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
